using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Acms.AcmsControls.ContentControls.FrontEnd
{
    /// <summary>
    /// Renders an Acms Image tag and a Hyperlink with Asp.Net Postback link
    /// </summary>
    public class AcmsImageLinkButton : ImageButton
    {
        /// <summary>
        /// Acms File Id to render
        /// </summary>
        public int? FileId { get; set; }

        /// <summary>
        /// Height to render the image
        /// </summary>
        public int RenderHeight { get; set; }

        /// <summary>
        /// Width to render the image
        /// </summary>
        public int RenderWidth { get; set; }

        protected override void OnPreRender(EventArgs e)
        {
            if (FileId != null && FileId.Value != 0)
            {
                this.ImageUrl = string.Format("~/File.aspx?Id={0}&width={1}&height={2}", FileId.Value, RenderWidth, RenderHeight);
                this.Visible = true;
            }
            else
            {
                this.Visible = false;
            }
            base.OnPreRender(e);
        }

        protected override object SaveViewState()
        {
            ViewState["FileId"] = FileId;
            ViewState["RenderWidth"] = RenderWidth;
            ViewState["RenderHeight"] = RenderHeight;
            return base.SaveViewState();
        }

        protected override void LoadViewState(object savedState)
        {
            base.LoadViewState(savedState);
            if (ViewState["FileId"] is int?)
            {
                FileId = (int?)ViewState["FileId"];
            }
            if (ViewState["RenderWidth"] is int)
            {
                RenderWidth = (int)ViewState["RenderWidth"];
            }
            if (ViewState["RenderHeight"] is int)
            {
                RenderHeight = (int)ViewState["RenderHeight"];
            }
        }
    }
}
